package cn.xiaoliu.quartz.service.impl;

import cn.xiaoliu.quartz.job.MainJob;
import cn.xiaoliu.quartz.service.IQuartzService;
import cn.xiaoliu.quartz.utils.QuartzJobInfo;
import cn.xiaoliu.quartz.utils.QuartzUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class QuartzServiceImpl implements IQuartzService {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    //添加定时任务
    @Override
    public void addJob(QuartzJobInfo jobInfo) {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        String jobName = jobInfo.getJobName();
        Class cls = MainJob.class;
        Map params = jobInfo.getParams();
        String time = jobInfo.getCronj();
        QuartzUtils.addJob(scheduler,jobName ,cls ,params ,time);
        System.out.println("动态添加定时任务"+jobName + "成功");
    }

    //删除定时任务
    @Override
    public void removeJob(String jobName) {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        QuartzUtils.removeJob(scheduler,jobName );
        System.out.println("删除掉没有执行的任务"+jobName);
    }
}
